//
//  YMAboutUsViewController.m
//  YMMedical
//
//  Created by y s on 2023/8/1.
//

#import "YMAboutUsViewController.h"
#import <WebKit/WebKit.h>
#import "YMMineService.h"


@interface YMAboutUsViewController ()<WKNavigationDelegate,WKUIDelegate>

@property (nonatomic, strong) WKWebView *webView;

@end

@implementation YMAboutUsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
    [self loadData];
}

#pragma mark -- UI
- (void)setUI{
    [self.view addSubview:self.webView];
    [self showNavTitle:KLanguage(@"about_us_txt")];
    self.hideNavLine = NO;
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void)loadData{
    WEAK_SELF;
    [YMMineService requestMineAboutUsSuccess:^(NSString * _Nonnull contentStr) {
        [weakSelf paramSuccess:contentStr];
    } failure:^(NSString * _Nonnull message) {
        [SHUD showInfo:message];
    }];
}

- (void)paramSuccess:(NSString *)contentStr{
    
    NSString *htmls = [NSString stringWithFormat:@"<html> \n"
                               "<head> \n"
                               "<style type=\"text/css\"> \n"
                               "body {font-size:15px;}\n"
                               "</style> \n"
                               "</head> \n"
                               "<body>"
                               "<script type='text/javascript'>"
                               "window.onload = function(){\n"
                               "var $img = document.getElementsByTagName('img');\n"
                               "for(var p in  $img){\n"
                                  " $img[p].style.width = '100%%';\n"
                                   "$img[p].style.height ='auto'\n"
                               "}\n"
                               "}"
                               "</script>%@"
                               "</body>"
                               "</html>",contentStr];

    [self.webView loadHTMLString:htmls baseURL:nil];
}


#pragma mark -- WKNavigationDelegate
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    
}
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error{

}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    
}


#pragma mark -- setter and getter
- (WKWebView *)webView {
    if (_webView == nil) {
        NSString *injectionJSString = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content','width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
        WKUserScript *injectionJSStringScript = [[WKUserScript alloc] initWithSource:injectionJSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        NSMutableString *javascript = [NSMutableString string];
        [javascript appendString:@"document.documentElement.style.webkitTouchCallout='none';"];//禁止长按
        [javascript appendString:@"document.documentElement.style.webkitUserSelect='none';"];//禁止选择
        WKUserScript *noneSelectScript = [[WKUserScript alloc] initWithSource:javascript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        WKUserContentController *userController = [WKUserContentController new];
        [userController addUserScript:injectionJSStringScript];
        [userController addUserScript:noneSelectScript];

        WKWebViewConfiguration *config          = [[WKWebViewConfiguration alloc]init];
        config.preferences                      = [WKPreferences new];
        config.preferences.minimumFontSize      = 10;
        config.preferences.javaScriptEnabled    = YES;
        config.preferences.javaScriptCanOpenWindowsAutomatically = YES;

        config.userContentController = userController;

        _webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
        _webView.navigationDelegate = self;
        if (@available(iOS 11.0, *)) {
            _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _webView;
}

@end
